package com.software.sqlite.api.admin.v1.controller;

import com.software.sqlite.api.admin.v1.service.impl.AmV1SysUserService;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.common.CommonContant;
import com.software.sqlite.entity.InitInfo;
import com.software.sqlite.utils.VerifyCodeUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Api(value = "管理端-登录", tags = "管理端-登录")
@RestController
@Slf4j
@RequestMapping("/admin/v1")
@CrossOrigin
public class LoginController {

    @Resource
    private AmV1SysUserService amV1SysUserService;

    @PostMapping("/loginInit")
    public AdminV1Result init(HttpServletRequest request)throws Exception{
        String secretKey=UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
        try {
            HttpSession session = request.getSession();
            session.setAttribute(CommonContant.USER_LOGIN_DES_CODE, secretKey);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return AdminV1Result.success(secretKey);
    }
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        //response.setContentType("img/jpeg");
        try {
            OutputStream outputStream=response.getOutputStream();
            int w = 200, h = 80;
            String code = VerifyCodeUtils.generateVerifyCode(4);
            HttpSession session = request.getSession();
            session.setAttribute(CommonContant.ADMIN_VERIFY_CODE_SESSION, code);
            VerifyCodeUtils.outputImage(w, h, outputStream, code);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "登录", notes = "登录")
    @GetMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "账号", required = true, defaultValue = "admin"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "123321"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, defaultValue = "123321")
    })
    public AdminV1Result<AdminLoginUserVO> login(@ApiParam(value = "账号") String userName,
                                                 @ApiParam(value = "密码") String password,
                                                 @ApiParam(value = "验证码") String verifyCode,
                                                 HttpServletRequest request){
        AdminLoginUserVO loginUser=amV1SysUserService.login(userName,password,verifyCode,request);
        AdminV1Result result=new AdminV1Result();
        result.setCode(loginUser.getUserId()==null?1:0);
        result.setMsg(loginUser.getMsg());
        result.setData(loginUser);
        return result;
    }

    @ApiOperation(value = "init", notes = "init")
    @GetMapping("/init")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, defaultValue = "token")
    })
    public InitInfo init(@ApiParam(value = "token") String token)throws Exception{
        return amV1SysUserService.getInitInfo(token);
    }

    @ApiOperation(value = "token", notes = "token")
    @GetMapping("/token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, defaultValue = "token")
    })
    public AdminV1Result token(@ApiParam(value = "token") String token){
        return AdminV1Result.success(amV1SysUserService.getTokenTime(token));
    }

}
